﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

namespace DGZfP.EventPublisher.Calendar
{
    public class EventsByDataTable : IEnumerable<IEvent>
    {
        DataTable Table { get; set; }
        string Context { get; set; }
        Dictionary<string, string> columnsMapping;

        public Dictionary<string, string> ColumnsMapping
        {
            get
            {
                if (columnsMapping == null)
                {
                    columnsMapping = EventByDataRow.DefaultColumnsMapping;
                }
                return columnsMapping;
            }
            set
            {
                columnsMapping = value;
            }
        }

        public EventsByDataTable(DataTable Table)
        {
            this.Table = Table;
            this.Context = "";
        }

        public EventsByDataTable(DataTable Table, string Context)
        {
            this.Table = Table;
            this.Context = Context;
        }

        public EventsByDataTable(DataTable Table, string Context, Dictionary<string, string> ColumnsMapping)
        {
            this.Table = Table;
            this.Context = Context;
            this.ColumnsMapping = ColumnsMapping;
        }


        public DateTime LowestDate
        {
            get
            {
                DataView dv = new DataView(Table, "", ColumnsMapping[EventByDataRow.StartColumn], DataViewRowState.CurrentRows);
                if (dv.Count > 0)
                    return (DateTime)dv[0][ColumnsMapping[EventByDataRow.StartColumn]];
                else
                    return DateTime.MinValue;
            }
        }

        #region IEnumerable<IEvent> Member

        public class MyEnumerator : IEnumerator<IEvent>
        {

            DataTable Table { get; set; }
            string Context { get; set; }
            Dictionary<string, string> ColumnsMapping { get; set; }
            int index;
            public MyEnumerator(DataTable Table, string Context, Dictionary<string, string> ColumnsMapping)
            {
                this.Table = Table;
                this.Context = Context;
                this.ColumnsMapping = ColumnsMapping;
                index = -1;
            }
            #region IEnumerator<IEvent> Member

            public IEvent Current
            {
                get { return new EventByDataRow(Table.Rows[index], Context, ColumnsMapping); }
            }

            #endregion

            #region IDisposable Member

            public void Dispose()
            {
            }

            #endregion

            #region IEnumerator Member

            object System.Collections.IEnumerator.Current
            {
                get { return new EventByDataRow(Table.Rows[index], Context); }
            }

            public bool MoveNext()
            {
                index++;
                if (index >= Table.Rows.Count) return false;
                return true;
            }

            public void Reset()
            {
                index = -1;
            }

            #endregion
        }

        public IEnumerator<IEvent> GetEnumerator()
        {
            return new MyEnumerator(Table, Context, ColumnsMapping);
        }

        #endregion

        #region IEnumerable Member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new MyEnumerator(Table, Context, ColumnsMapping);
        }

        #endregion
    }
}
